import 'package:fig/models/media.dart';
import 'package:fig/sqlite/table_media.dart';
import 'package:fig/views/widget/media_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CategoryDetailWidget extends StatefulWidget {
  final int categoryId;

  CategoryDetailWidget(this.categoryId, {Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _CategoryDetailWidget();
}

class _CategoryDetailWidget extends State<CategoryDetailWidget> {
  final crossAxisCount = 3;

  List<Media> mediaList = [];

  void loadData(int page) {
    TableMedia().pageByCategory(widget.categoryId, page, 20).then((_data) {
      if (mounted) {
        setState(() {
          if (page == 1) {
            mediaList.clear();
          }
          mediaList.addAll(_data);
        });
      }
    });
  }

  @override
  void initState() {
    super.initState();

    loadData(1);
  }

  Future<void> _onRefresh() async {
    // 刷新界面 setState
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: GridView.count(
      // 每一行展示几个
      crossAxisCount: crossAxisCount,
      // 纵向间距
      mainAxisSpacing: 12,
      // 横向间距
      crossAxisSpacing: 12,
      // 整体的padding
      padding: const EdgeInsets.only(left: 14, right: 14, top: 10, bottom: 10),
      // item的宽高比
      childAspectRatio: 0.618,
      children: mediaList.map<Widget>((media) {
        return MediaItem(media, crossAxisCount: crossAxisCount);
      }).toList(),
    ));
  }
}
